package com.borqs.samples.developer;

import java.util.Map;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

public class PreferencesUtil {

    private Context mContext;

    public PreferencesUtil(Context context) {
        this.mContext = context;
    }

    /**
     * SharedPreferences ��ʼ��
     * 
     * @return
     */
    private SharedPreferences getSharedPreferences() {
        return mContext.getSharedPreferences("coupon", Context.MODE_APPEND);
    }

    /**
     * SharedPreferences �洢string�������
     * 
     * @param key
     * @param value
     */
    public void putString(String key, String value) {
        SharedPreferences sharedPreferences = getSharedPreferences();
        Editor editor = sharedPreferences.edit();
        editor.putString(key, value);
        editor.commit();
    }

    /**
     * SharedPreferences �洢int�������
     * 
     * @param key
     * @param value
     */
    public void putInt(String key, int value) {
        SharedPreferences sharedPreferences = getSharedPreferences();
        Editor editor = sharedPreferences.edit();
        editor.putInt(key, value);
        editor.commit();
    }

    /**
     * SharedPreferences �洢boolean�������
     * 
     * @param key
     * @param value
     */
    public void putBoolean(String key, Boolean value) {
        SharedPreferences sharedPreferences = getSharedPreferences();
        Editor editor = sharedPreferences.edit();
        editor.putBoolean(key, value);
        editor.commit();
    }

    /**
     * SharedPreferences ��ȡstring�������
     * 
     * @param key
     * @return
     */
    public String getString(String key) {
        SharedPreferences sharedPreferences = getSharedPreferences();
        return sharedPreferences.getString(key, null);
    }

    /**
     * SharedPreferences ��ȡint�������
     * 
     * @param key
     * @return
     */
    public int getInt(String key) {
        SharedPreferences sharedPreferences = getSharedPreferences();
        return sharedPreferences.getInt(key, 0);
    }

    /**
     * SharedPreferences ��ȡboolean�������
     * 
     * @param key
     * @return
     */
    public boolean getBoolean(String key) {
        SharedPreferences sharedPreferences = getSharedPreferences();
        return sharedPreferences.getBoolean(key, false);
    }

    /**
     * SharedPreferences ��ȡobject�������
     * 
     * @param key
     * @return
     */
    public Object getValue(String key) {
        SharedPreferences sharedPreferences = getSharedPreferences();
        Map<String, ?> map = sharedPreferences.getAll();
        if (map != null && !map.isEmpty()) {
            return map.get(key);
        }
        return null;
    }
}
